package com.booby.generic;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * 功能描述: 泛型擦除
 *
 * @author: lizt
 * @date: 2020/8/16 15:14
 **/

public class GenericErasure {

    public static void main(String[] args) {

        GenericErasure erasure = new GenericErasure();
        /**************  泛型类型判断  *********************/
        List<String> strList = new ArrayList<>();
        List<Integer> integerList = new ArrayList<>();
        // true 说明类型与泛型无关
        System.out.println(strList.getClass().equals(integerList.getClass()));

        /**************** 参数带泛型的类名 *******************/
        erasure.erasureObject();

        /**************** 泛型的类名为上线通配符 *******************/
        erasure.erasureUpperBoundary();

        /**************** 泛型的类名为下线通配符 *******************/
        erasure.erasureLowerBoundary();



    }

    public void erasureObject(){
        Erasure<String> erasure = new Erasure<>("Hello");
        Class clazz = erasure.getClass();
        // 类名不带有泛型
        System.out.println("Erasure class is:" + clazz);
        Field[] fs = clazz.getDeclaredFields();
        for (Field f : fs){
            // 字段类型为Object
            System.out.println("Erasure name "+f.getName()+" type:"+f.getType().getName());
        }
    }

    public void erasureUpperBoundary(){
        ErasureUpperBoundary<String> erasure = new ErasureUpperBoundary<>("Hello");
        Class clazz = erasure.getClass();
        // 类名不带有泛型
        System.out.println("ErasureUpperBoundary class is:" + clazz);
        Field[] fs = clazz.getDeclaredFields();
        for (Field f : fs){
            // 字段类型为String
            System.out.println("ErasureUpperBoundary Field name "+f.getName()+" type:"+f.getType().getName());
        }
    }

    public void erasureLowerBoundary(){
        ErasureLowerBoundary<String> erasure = new ErasureLowerBoundary<>("Hello");
        Class clazz = erasure.getClass();
        // 类名不带有泛型
        System.out.println("ErasureLowerBoundary class is:" + clazz);
        Field[] fs = clazz.getDeclaredFields();
        for (Field f : fs){
            // 字段类型为String
            System.out.println("ErasureLowerBoundary Field name "+f.getName()+" type:"+f.getType().getName());
        }
    }

}
class  Erasure<T>{
    private T param;

    public Erasure(T param) {
        this.param = param;
    }
}

class  ErasureUpperBoundary<T extends String>{
    private T param;

    public ErasureUpperBoundary(T param) {
        this.param = param;
    }
}

class  ErasureLowerBoundary<T extends String>{
    private T param;

    public ErasureLowerBoundary(T param) {
        this.param = param;
    }
}

